package com.health.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.health.R;
import com.health.tablebean.BD_DD_INFO;
import com.health.tools.GlobalConts;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

/**
 * 
 * @author 鹿云龙
 *
 */
public class CheckBoxAdapter_EatHobby extends BaseAdapter {
	private Context ctx;
	private List<BD_DD_INFO> list;
	private List<CheckViewHolder> holderList;
	private LayoutInflater layoutInflater;
	private HashMap<Integer, CheckViewHolder> checkMap;
	public CheckBox checkBox;
	/**
	 * 设置选中的CheckBox
	 */
	private int mtextSize;
	private int mtextColor;
	private boolean textsizech;
	private boolean textcolorch;

	private static boolean check1 = false;
	private static boolean check2 = false;
	private static boolean check3 = false;

	@SuppressLint("UseSparseArrays")
	public CheckBoxAdapter_EatHobby(Context context, List<BD_DD_INFO> list) {
		this.list = list;
		this.ctx = context;
		layoutInflater = LayoutInflater.from(context);
		checkMap = new HashMap<Integer, CheckViewHolder>();
		holderList = new ArrayList<>();
	}

	/**
	 * 数据总数
	 */
	@Override
	public int getCount() {
		return list.size();
	}

	/**
	 * 获取当前数据
	 */
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (layoutInflater != null) {
			convertView = layoutInflater.inflate(R.layout.checkboxitem, null);
			// TextView textView = (TextView) view.findViewById(R.id.title);
			CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);
			EditText et = (EditText) convertView.findViewById(R.id.othertext);
			// 获取自定义的类实例
			BD_DD_INFO tempGridViewItem = list.get(position);
			// android:focusable="false"
			CheckViewHolder avh = new CheckViewHolder();
			avh.cbIamge = cb;
			avh.choseView = convertView;
			avh.otherText = et;
			avh.dd_info = tempGridViewItem;
			if (textsizech) {
				avh.cbIamge.setTextSize(mtextSize);
			}
			if (textcolorch) {
				avh.cbIamge.setTextColor(mtextColor);
			}
			cb.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl(avh, position));
			cb.setFocusable(false);
			cb.setText(tempGridViewItem.getDATAINFO());

			checkMap.put(position, avh);
			holderList.add(avh);

			setChecked(chekedsStr);

			if (checkMap.size() == list.size()) {
				ballLoad = true;
			}
		}
		return convertView;
	}

	/**
	 * 加载完所有的CheckBox
	 */
	boolean ballLoad;

	final class CheckViewHolder {
		CheckBox cbIamge;
		View choseView;
		EditText otherText;
		BD_DD_INFO dd_info;
	}

	private String chekedsStr;

	/**
	 * 设置选中项
	 * 
	 * @param checkeds
	 *            选中对象的id,以英文逗号分隔(例："DM_001,DM_002,DM_003...")
	 * @author 鹿云龙
	 * @date 2015年12月21日 上午9:36:49
	 */
	public void setChecked(String checkeds) {
		if (ballLoad) {
			return;
		}
		chekedsStr = checkeds;
		if (checkeds == null || checkeds.equals("")) {
			return;
		}
		if (checkMap.size() == 0) {
			for (int i = 0; i < holderList.size(); i++) {
				checkMap.put(i, holderList.get(i));
			}
		}
		String[] cks = checkeds.split(",");
		Object[] keys = checkMap.keySet().toArray();
		for (int i = 0; i < checkMap.size(); i++) {
			CheckViewHolder cvh = checkMap.get((int) keys[i]);
			for (String ck : cks) {
				if (cvh.dd_info.getID().equals(ck)) {
					if (id1.equals(cvh.dd_info.getID())) {
						check1 = true;
					} else if (id2.equals(cvh.dd_info.getID())) {
						check2 = true;
					} else if (id3.equals(cvh.dd_info.getID())) {
						check3 = true;
					}
					cvh.cbIamge.setChecked(true);
				}
			}
		}
	}

	/**
	 * size The scaled pixel size.
	 * 
	 * @param size
	 * @author 鹿云龙
	 * @date 2015年12月18日 下午2:36:29
	 */
	public void setTextSize(int textSize) {
		this.mtextSize = textSize;
		textsizech = true;
	}

	/**
	 * Sets the text color.
	 * 
	 * @param colors
	 * @author 鹿云龙
	 * @date 2015年12月18日 下午2:36:36
	 */
	public void setTextColor(int textColors) {
		this.mtextColor = textColors;
		textcolorch = true;
	}

	/**
	 * 获取选中对象的id ,使用英文逗号隔开
	 * 
	 * @return 1,2,3...
	 * @author 鹿云龙
	 * @date 2015年12月17日 上午11:16:44
	 */
	public String getCheckedIdsStr() {
		String str = "";
		Object[] keys = checkMap.keySet().toArray();
		if (keys.length != 0) {
			for (int i = 0; i < checkMap.size(); i++) {
				CheckViewHolder cvh = checkMap.get((int) keys[i]);
				if ("DM100-06_1".equals(cvh.dd_info.getID())) {
					if (check1) {
						str += cvh.dd_info.getID() + ",";
					}
				} else if (cvh.cbIamge.isChecked()) {
					str += cvh.dd_info.getID() + ",";
				}
			}
			if (str.length() != 0) {
				str = str.substring(0, str.length() - 1);
			}
		}
		Log.e("ddddddddd", str);
		return str;
	}

	/**
	 * 获取选中的BD_DD_INFO对象
	 * 
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月17日 上午11:16:53
	 */
	public ArrayList<BD_DD_INFO> getCheckedBD_DD_INFO() {
		// checkedIds.clear();
		ArrayList<BD_DD_INFO> arr = new ArrayList<BD_DD_INFO>();
		Object[] keys = checkMap.keySet().toArray();
		for (int i = 0; i < checkMap.size(); i++) {
			CheckViewHolder cvh = checkMap.get((int) keys[i]);
			if (cvh.cbIamge.isChecked()) {
				arr.add(cvh.dd_info);
			}
		}
		return arr;
	}

	private static final String id1 = "DM100-06_1";
	private static final String id2 = "DM100-06_2";
	private static final String id3 = "DM100-06_3";

	class OnCheckedChangeListenerImpl implements CompoundButton.OnCheckedChangeListener {
		CheckViewHolder holder;
		int position;

		public OnCheckedChangeListenerImpl(CheckViewHolder holder, int position) {
			super();
			this.holder = holder;
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			try {
				Log.e("选中结果--pos--", "" + position);
				String checkboxtext = holder.cbIamge.getText().toString();
				Log.e("选中结果--111--", checkboxtext);
				String str_id = holder.dd_info.getID();
				Log.e("选中结果--111--", str_id);
				if (ballLoad) {
					if (isChecked) {
						if (str_id.equals(id1)) {
							check1 = true;
							if (check2 || check3) {
								// 说明不是选中了一个
								holder.cbIamge.setChecked(false);
								check1 = false;
								GlobalConts.showTextDialog(ctx, "荤素均衡、荤食为主、素食为主相互排斥，不可同时选择");
							}
						} else if (str_id.equals(id2)) {
							check2 = true;
							if (check1 || check3) {
								// 说明不是选中了一个
								holder.cbIamge.setChecked(false);
								check2 = false;
								GlobalConts.showTextDialog(ctx, "荤素均衡、荤食为主、素食为主相互排斥，不可同时选择");
							}
						} else if (str_id.equals(id3)) {
							check3 = true;
							if (check1 || check2) {
								// 说明不是选中了一个
								holder.cbIamge.setChecked(false);
								check3 = false;
								GlobalConts.showTextDialog(ctx, "荤素均衡、荤食为主、素食为主相互排斥，不可同时选择");
							}
						}
					} else {
						if (str_id.equals(id1)) {
							check1 = false;
						} else if (str_id.equals(id2)) {
							check2 = false;
						} else if (str_id.equals(id3)) {
							check3 = false;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}